package com.example.nna.main;

import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.example.nna.listview.listViewCustom1;
import com.example.nna.listview.listViewCustom1.OnActionItemClickListener;
import com.example.nna.quickaction.ActionItem;
import com.example.nna.quickaction.QuickAction;
import com.example.nna.voz.Voz;
import com.example.nna.contain.containPage1;

public class Page1 extends Page{
	private ArrayList<containPage1> ListContains;
	private listViewCustom1 adapter;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.page1);
		super.onCreate(savedInstanceState);
		
		ListContains = new ArrayList<containPage1>();
		adapter = new listViewCustom1(mActivity,ListContains);
	    adapter.setSize(mTextSize);
	    
		mList.setAdapter(adapter);
		mTask.execute(TASK_GETDOC);
		adapter.setOnActionItemClickListener(new OnActionItemClickListener() {
			
			@Override
			public void onItemClick(int pos, int actionId) {
				// TODO Auto-generated method stub
				if(actionId==1){
					toast(ListContains.get(pos).Forum(),0,0);
					Intent i = new Intent(mContext, Page2.class);
					i.putExtra("URL", ListContains.get(pos).UrlForum());
					i.putExtra("TITLE", ListContains.get(pos).Forum());
					startActivity(i);
					overridePendingTransition(R.anim.rail, 
											  R.anim.rail);
				}
				else if(actionId == 2) toast(ListContains.get(pos).LastPost(),0,0);
			}
		});
		
		
	}
	@Override
	void initUI() {
		// TODO Auto-generated method stub
		mList = (ListView)findViewById(R.id.mainlist);
		super.initUI();
	}
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_parser_html, menu);
		return super.onCreateOptionsMenu(menu);
	}
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case R.id.menu_login:
			alertLogin();
			return true;
		case R.id.menu_font:
			alertFont();
			return true;	
	    default:
	    	return super.onOptionsItemSelected(item);
	    }
	}
	@Override
	protected void updateSetFont(float value) {
		// TODO Auto-generated method stub
		super.updateSetFont(value);
		adapter.setSize(value);
		adapter.notifyDataSetChanged();
	}

	@Override
	void Parser() {
		// TODO Auto-generated method stub
		ListContains.clear();
		super.Parser();
		Bitmap bitmap = null;
		String forum = null,urlForum=null,urlLastPost=null, lastpost = null;
		
		if(doc == null){
			toast("Cannot connect Vozforums",1,0);
			return;
		}
		Element eleLogin = doc.select("a[href=private.php]").first();
		if(eleLogin != null){
			String temp = doc.select("td[class=alt2]").get(0).select("a[href*=mem]").attr("href");
			temp = temp.split("=")[1];
			mUser.setUserId(temp);
		}
		Elements eleTag1s = doc.select("td[class*=tcat]");
		Iterator<Element> iteTag1 = eleTag1s.iterator();

		while(iteTag1.hasNext()){
			Element eleTag1 = iteTag1.next();
			if(eleTag1.text().contains("Welcome to the vozForums")) continue;
			ListContains.add(new containPage1(eleTag1.text(),  null,null,null,
					null,eleTag1.select("[href~=forum]").attr("href"),null));
			
			Element eleTag2 = eleTag1.parent().parent().nextElementSibling();
			if(eleTag2 != null){
				Elements eleTag3s = eleTag2.select("tr");
				Iterator<Element> iteTag3 = eleTag3s.iterator();
				while(iteTag3.hasNext()){
					Element eleTag4 = iteTag3.next();
					Elements eleTag5 = eleTag4.select("td");    
					
				    if(eleTag5.size()!= 1){
				    	if(eleTag5.get(0).select("img").attr("src") != null){	
				    		bitmap = loadBitmapAssert(eleTag5.get(0).select("img").attr("src")); 
				    	}
				    	
				    	if(eleTag5.get(2) != null && !eleTag5.get(2).text().equals("Never")
				    			&& eleTag5.get(2).child(0) != null){
	   	  			    	Elements eleTag6s = eleTag5.get(2).child(0).select("div");
	   	  			    	lastpost= eleTag6s.get(1).text() + "\n";
	   	  			    	lastpost += "by " + eleTag6s.get(2).select("a[rel]").text() + " " ;
	   	  			    	lastpost += eleTag6s.get(3).select("div").text();
				    	}
				    	
   	  			    	if(eleTag5.get(1) != null)
   	  			    		forum = eleTag5.get(1).select("td[class*=alt1Active]").text();
   	  			    	//log(forum);
   	  			    	if(eleTag5.get(1).select("a[href]").first()!= null){
   	  			    		urlForum = eleTag5.get(1).select("a[href]").attr("href");
   	  			    	}
   	  			    	if(eleTag5.get(2).select("a[href]").first() != null){
   	  			    		urlLastPost =  eleTag5.get(2).select("a[href]").get(1).attr("href");
   	  			    	}
    	  			    ListContains.add(new containPage1(forum,lastpost,eleTag5.get(3).text(),eleTag5.get(4).text(),bitmap,
       	   	  				urlForum,urlLastPost));
       	   	  		
       	   	  			adapter.notifyDataSetChanged();
				    }
				}
			}
		}
		log("task parsered finish");
		
	}
	

}
